﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    class Object2D
    {
        private Vector2 position;
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        private Texture2D image;
        public Texture2D Image
        {
            get { return image; }
            set { image = value; }
        }

        private Rectangle rect;
        public Rectangle Rect
        {
            get { return rect; }
            set { rect = value; }
        }

        private Color cor;
        public Color Cor
        {
            get { return cor; }
            set { cor = value; }
        }

        private float rotation;
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        private Vector2 origin;
        public Vector2 Origin
        {
            get { return origin; }
            set { origin = value; }
        }

        private Vector2 scale = Vector2.One; //retorna um vetor onde x e y são igual a 1 (default é zero)
        public Vector2 Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        private SpriteEffects effects;
        public SpriteEffects Effects
        {
            get { return effects; }
            set { effects = value; }
        }

        private float layerDepth;
        public float LayerDepth
        {
            get { return layerDepth; }
            set { layerDepth = value; }
        }

        private bool isVisible = true;
        public bool IsVisible
        {
            get { return isVisible; }
            set { isVisible = value; }
        }

        public Object2D(Texture2D image, Vector2 position)
        {
            this.image = image;
            this.position = position;
        }

        public bool CheckCollision(Object2D obj)
        {
            if (Position.X - Rect.Width / 2 <= obj.Position.X + obj.Rect.Width / 2 && obj.Position.X + obj.Rect.Width / 2 <= Position.X + Rect.Width / 2)
            {
                return true;
            }
            else if (Position.X + Rect.Width / 2 <= obj.Position.X + obj.Rect.Width / 2 && obj.Position.X + obj.Rect.Width / 2 <= Position.X - Rect.Width / 2)
            {
                return true;
            }
            else if (Position.Y - Rect.Height / 2 <= obj.Position.Y + obj.Rect.Height / 2 && obj.Position.Y + obj.Rect.Height / 2 <= Position.Y + Rect.Height / 2)
            {
                return true;
            }
            else if (Position.Y + Rect.Height / 2 <= obj.Position.Y + obj.Rect.Height / 2 && obj.Position.Y + obj.Rect.Height / 2 <= Position.Y - Rect.Height / 2)
            {
                return true;
            }
            return false;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(image, position, Color.White);
        }
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (isVisible) spriteBatch.Draw(image, position, rect, cor, rotation, origin, scale, effects, layerDepth);
        }
    }

    public class AnimatedObject2D
    {
        private Vector2 position;
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        private Texture2D image;
        public Texture2D Image
        {
            get { return image; }
            set { image = value; }
        }

        private Rectangle rect;
        public Rectangle Rect
        {
            get { return rect; }
            set { rect = value; }
        }

        private Color cor;
        public Color Cor
        {
            get { return cor; }
            set { cor = value; }
        }

        private float rotation;
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        private Vector2 origin;
        public Vector2 Origin
        {
            get { return origin; }
            set { origin = value; }
        }

        private Vector2 scale = Vector2.One; //retorna um vetor onde x e y são igual a 1 (default é zero)
        public Vector2 Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        private SpriteEffects effects;
        public SpriteEffects Effects
        {
            get { return effects; }
            set { effects = value; }
        }

        private float layerDepth;
        public float LayerDepth
        {
            get { return layerDepth; }
            set { layerDepth = value; }
        }

        private bool isVisible = true;
        public bool IsVisible
        {
            get { return isVisible; }
            set { isVisible = value; }
        }

        private Animation[] animations;
        private Animation currentAnimation;
        public Animation CurrentAnimation
        {
            get { return currentAnimation; }
            set { currentAnimation = value; }
        }
        private bool EntitySet;
        private EntityType myEntity = EntityType.Neutral;
        public EntityType MyEntity
        {
            get { return myEntity; }
            set
            {
                if (!EntitySet)
                {
                    EntitySet = true;
                    myEntity = value;
                }
            }
        }

        public AnimatedObject2D(Texture2D image, Vector2 position, Animation[] animations)
        {
            this.image = image;
            this.position = position;
            this.rotation = 0f;
            this.animations = animations;
            this.currentAnimation = animations[0];
            this.rect = currentAnimation.Frame;
            this.origin = new Vector2(rect.Width / 2, rect.Height / 2);
            this.layerDepth = 0f;
            this.effects = SpriteEffects.None;
            this.cor = Color.White;
            this.scale = Vector2.One;
        }

        public virtual void Update(GameTime gameTime)
        {
            if (currentAnimation.nextFrame < (float)gameTime.TotalGameTime.TotalMilliseconds)
            {
                currentAnimation.nextFrame += currentAnimation.FrameRate;
                currentAnimation.currentFrame.X++;
                if (currentAnimation.currentFrame.X >= currentAnimation.TotalFrames.X)
                {
                    currentAnimation.currentFrame.X = 0;

                    currentAnimation.currentFrame.Y++;
                    if (currentAnimation.currentFrame.Y >= currentAnimation.TotalFrames.Y)
                    {
                        currentAnimation.currentFrame.Y = 0;

                    }
                }
                this.rect = new Rectangle((int)
                    (currentAnimation.startPosition.X +
                    currentAnimation.FrameSize.X * currentAnimation.currentFrame.X),
                    (int)(
                    currentAnimation.startPosition.Y +
                    currentAnimation.FrameSize.Y * currentAnimation.currentFrame.Y),
                    (int)(
                    currentAnimation.FrameSize.X),
                    (int)(
                    currentAnimation.FrameSize.Y));
            }
        }

        public bool CheckCollision(AnimatedObject2D obj)
        {
            if (Position.X - Rect.Width / 2 <= obj.Position.X + obj.Rect.Width / 2 && obj.Position.X + obj.Rect.Width / 2 <= Position.X + Rect.Width / 2)
            {
                return true;
            }
            else if (Position.X + Rect.Width / 2 <= obj.Position.X + obj.Rect.Width / 2 && obj.Position.X + obj.Rect.Width / 2 <= Position.X - Rect.Width / 2)
            {
                return true;
            }
            else if (Position.Y - Rect.Height / 2 <= obj.Position.Y + obj.Rect.Height / 2 && obj.Position.Y + obj.Rect.Height / 2 <= Position.Y + Rect.Height / 2)
            {
                return true;
            }
            else if (Position.Y + Rect.Height / 2 <= obj.Position.Y + obj.Rect.Height / 2 && obj.Position.Y + obj.Rect.Height / 2 <= Position.Y - Rect.Height / 2)
            {
                return true;
            }
            return false;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(image, position, Color.White);
        }
        public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (isVisible) spriteBatch.Draw(image, position, rect, cor, rotation, origin, scale, effects, layerDepth);
        }


    }
    public struct Animation
    {
        public Vector2 startPosition;
        public string Name;
        public Vector2 currentFrame;
        public Vector2 TotalFrames;
        public Rectangle Frame;
        public Vector2 FrameSize;
        private float rate;
        public float FrameRate
        {
            set { rate = value; }
            get { return 1000 / rate; }
        }
        public float nextFrame;
    }
    public enum EntityType
    {
        Friendly, Hositile, Neutral, Both, All
    }
}
